package org.br.plotter.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Rectangle;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EqualComponentLayoutManager implements LayoutManager {

	private boolean vertical;
	private int freeSpace;

	public EqualComponentLayoutManager() {
		this(false, 0);
	}

	public EqualComponentLayoutManager(boolean vertical) {
		this(vertical, 0);
	}

	public EqualComponentLayoutManager(boolean vertical, int freeSpace) {
		this.vertical = vertical;
		this.freeSpace = freeSpace;
	}

	public void addLayoutComponent(String arg0, Component arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void layoutContainer(Container container) {
		if (vertical) {
			layoutVerticalContainer(container);
		} else {
			layoutHorizoContainer(container);
		}
	}

	public void layoutVerticalContainer(Container container) {
		Component[] comps = container.getComponents();
		Rectangle all = container.getBounds();
		int restSpace = (freeSpace * all.height) / 100;
		int height = all.height - restSpace;
		int width = all.width;
		int length = comps.length;
		int allHeight = restSpace;
		if (length > 0) {
			height = height / length;
			for (int i = 0; i < length; i++) {
				Component cmp = comps[i];
				cmp.setBounds(0, allHeight, width, height);
				allHeight += height;
			}
		}
	}

	public void layoutHorizoContainer(Container container) {
		Component[] comps = container.getComponents();
		Rectangle all = container.getBounds();
		int restSpace = (freeSpace * all.width) / 100;
		int height = all.height;
		int width = all.width - restSpace;
		int length = comps.length;
		int allWidth = restSpace;
		if (length > 0) {
			width = width / length;
			for (int i = 0; i < length; i++) {
				Component cmp = comps[i];
				cmp.setBounds(allWidth, 0, width, height);
				allWidth += width;
			}
		}
	}

	@Override
	public Dimension minimumLayoutSize(Container container) {
		Component[] comps = container.getComponents();
		int length = comps.length;
		if (length > 0) {
			Dimension dimension = comps[0].getMinimumSize();
			return returnLayoutSize(dimension, length);
		}
		return new Dimension();
	}

	@Override
	public Dimension preferredLayoutSize(Container container) {
		Component[] comps = container.getComponents();
		int length = comps.length;
		if (length > 0) {
			Dimension dimension = comps[0].getPreferredSize();
			return returnLayoutSize(dimension, length);
		}
		return new Dimension();
	}

	private Dimension returnLayoutSize(Dimension elementSize, int elementnumber) {
		return vertical ? returnVerticalLayoutSize(elementSize, elementnumber)
				: returnHorizontalLayoutSize(elementSize, elementnumber);
	}

	protected Dimension returnVerticalLayoutSize(Dimension elementSize,
			int elementnumber) {
		int width = elementSize.width;
		int h = elementSize.height;
		int height = calculate(h, elementnumber);
		return new Dimension(width, height);
	}

	private int calculate(int h, int elementnumber) {
		int height = h * elementnumber;
		float percentage = (100 - freeSpace) / 100;
		height = (int) (height / percentage);
		return height;
	}

	private Dimension returnHorizontalLayoutSize(Dimension elementSize,
			int elementnumber) {
		int w = elementSize.width;
		int width = calculate(w, elementnumber);
		;
		int height = elementSize.height;
		return new Dimension(width, height);
	}

	@Override
	public void removeLayoutComponent(Component arg0) {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		JDialog dialog = new JDialog();
		JPanel panel = new JPanel();
		panel.add(new JLabel("Shadi"));
		// panel.add(new JLabel("Shadi"));
		panel.add(new JTextField());
		dialog.setModal(true);
		dialog.setResizable(false);
		panel.setLayout(new EqualComponentLayoutManager(false, 75));
		dialog.pack();
		dialog.add(panel);
		dialog.setBounds(200, 200, 300, 300);
		dialog.setVisible(true);
	}

}
